package com.vcg.ratpack.config;

import com.vcg.ratpack.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ratpack.func.Action;
import ratpack.func.Block;
import ratpack.handling.ByMethodSpec;
import ratpack.handling.internal.AcceptsHandler;
import ratpack.server.RatpackServer;
import ratpack.server.RatpackServerSpec;
import ratpack.server.ServerConfig;
import ratpack.spring.Spring;

import static ratpack.spring.Spring.spring;

/**
 * Created by wuyu on 2016/9/17.
 */
@Configuration
public class RatpackConfiguration {

    @Autowired
    ApplicationContext applicationContext;

    @Bean
    public RatpackServer ratpackServer() throws Exception {
        RatpackServer reRatpackServer = RatpackServer.start(server -> server
                .registry(spring(applicationContext))
                .handlers(chain -> chain
                        .get(":message", ctx -> ctx
                                .render("Hello " + ctx.getPathTokens().get("message") + "!")
                        )
                )
                .serverConfig(ServerConfig.builder()
                        .threads(200)
                        .port(5050)));
        RatpackServer.start(new Action<RatpackServerSpec>() {
            @Override
            public void execute(RatpackServerSpec ratpackServerSpec) throws Exception {

            }
        });
        return  reRatpackServer;

    }

    @Bean
    public ServerConfig serverConfig() {
        return ServerConfig.builder()
                .threads(200)
                .port(5050).build();
    }
}
